const path = require('path')
const webpack = require('webpack')
const HtmlPrettyConsoleWebpackPlugin = require('html-pretty-console-webpack-plugin')
const MyCustomPlugin = require('./plugins/dlts-webpack-plugin.js')

module.exports = {
  productionSourceMap: false,
  pluginOptions: {
    electronBuilder: {
      builderOptions: {
        appId: 'com.learn.app',
        productName: 'Learn', // 项目名，也是生成的安装文件名，即 electron.exe
        asar: true,
        copyright: 'Copyright © 2023 learn.com All Rights Reserved', // 版权信息
        directories: {
          output: 'release' // 输出文件路径
        },
        win: {
          icon: 'dist_electron/icons/icon.ico',
          target: [{
            target: 'nsis', // 利用nsis制作安装程序
            arch: ['x64', 'ia32']
          }]
        },
        nsis: {
          allowElevation: true, // 允许请求提升
          oneClick: false, // 是否一键安装
          allowToChangeInstallationDirectory: true, // 允许修改安装目录
          installerIcon: 'dist_electron/icons/icon.ico', // 安装图标
          uninstallerIcon: 'dist_electron/icons/icon.ico', // 卸载图标
          installerHeaderIcon: 'dist_electron/icons/icon.ico', // 安装时头部图标
          createDesktopShortcut: true, // 创建桌面图标
          createStartMenuShortcut: true, // 创建开始菜单图标
          shortcutName: 'electron-learn', // 图标名称
          runAfterFinish: false // 是否安装完成后运行
          // perMachine: true, // 是否开启安装时权限限制
          // guid: 'electron-learn',
          // include: 'build/installer.nsh', // 包含的自定义nsis脚本
        },
        dmg: {
          icon: 'dist_electron/icons/icon.icns',
          iconSize: 100, // 图标的尺寸
          contents: [
            {
              x: 410,
              y: 150,
              type: 'link',
              path: '/Applications'
            },
            {
              x: 130,
              y: 150,
              type: 'file'
            }
          ]
        },
        // pkg: {
        //   welcome: 'Hello Learn'
        // },
        mac: {
          icon: 'dist_electron/icons/icon.icns',
          // identity: 'xxxxx',
          category: 'public.app-category.utilities', // 应用类型
          hardenedRuntime: true,
          gatekeeperAssess: false,
          target: ['dmg', 'zip', 'pkg'],
          darkModeSupport: true
        },
        publish: [{
          provider: 'generic',
          url: 'http://192.168.101.112:8080/electrons/'
        }],
        // files: ['**/*', '!node_modules/**/*'],
        files: ['!node_modules/**/*'],
        beforeBuild (context) {
          console.log(context)
          console.log('-----------------')
          return null
        }
      },
      nodeIntegration: true,
      preload: path.join(__dirname, 'src/preload.ts'),
      chainWebpackMainProcess: (config) => {
        config.plugin('env').tap(args => {
          args[0].LG_CY_NAME = 'LG_CY_NAME'
          return args
        })
      }
    }
  },
  configureWebpack: {
    mode: process.env.NODE_ENV
  },
  chainWebpack: config => {
    // https://blog.csdn.net/qq_41887214/article/details/129567640
    // config.module
    //   .rule('ts')
    //   .use('transfer-loader')
    //   .loader('./loaders/transfer-loader.js')
    //   .before('babel-loader')
    //   .end()

    config.module
      .rule('transfer')
      .test(/\.ts$/)
      .use('cache-loader')
      .loader('cache-loader')
      .end()
      .use('transfer-loader')
      .loader('./loaders/transfer-loader.js')
      .after('babel-loader')
      .tap(() => {
        return {
          name: '[chunkhash:8]'
        }
      })
      .end()
      .use('babel-loader')
      .loader('babel-loader')
      .end()
      .use('ts-loader')
      .loader('ts-loader')
      .tap(() => {
        return {
          transpileOnly: true,
          appendTsSuffixTo: [
            '\\.vue$'
          ],
          happyPackMode: false
        }
      })
      .end()
      .include
      .add(path.resolve('./src/components'))
      .end()

    // https://blog.csdn.net/qq_41887214/article/details/129567640
    config.plugin('define').tap(args => {
      console.log('----- define -----');
      const stVal = args[0]
      return [ { 'process.env': { ...stVal['process.env'], NEW_SMILE: '2024'}} ]
    })

    config.plugin('re-define').use(webpack.DefinePlugin, [
      {
        'process.env': {
          CURRENT_LOGIN: Date.now(),
          ELECTRON_IS_AUTH: true,
          ELECTRON_NAME: '"Smile Electron"'
        }
      }
    ]).before('define')


    config.plugin('env').use(webpack.EnvironmentPlugin, [{
      LY_SMILE_NAME: 'LY_SMILE_NAME'
    }])

    config.plugin('console').use(HtmlPrettyConsoleWebpackPlugin, [{
      output: [
        { label: '分支信息', value: 'master' },
        { label: '打包时间', value: new Date().toString() }
      ]
    }])

    config.plugin('custom-plugin').use(MyCustomPlugin).after('vue-loader')

    // 查看框架内已配置的 Plugins VS Loaders
    const webpackConfig = config.toConfig()
    const plugins = webpackConfig.plugins
    console.log(config.module.rules.store.keys())
    console.log(config.plugins.store.keys())
    // console.log(webpackConfig.module.rules);
    console.log('=== Used Plugins ===', plugins.length)
    console.log(plugins.map(plugin => plugin.constructor.name))
  }
}
